//
//  YWAnchorIntroduceViewController.m
//  聆听FM
//
//  Created by dllo on 16/6/14.
//  Copyright © 2016年 Dark. All rights reserved.
//

#import "YWAnchorIntroduceViewController.h"
#import "YWAnchorMessageModel.h"
#import "YWHeaderView.h"
#import "ZCSTipsReTableViewCell.h"
#import "YWAnchorIntroduceAlbumsModel.h"
#import "YWAnchorIntroduceAlbumTableViewCell.h"
#import "YWAnchorVoiceModel.h"
#import "YWAnchorVoiceTableViewCell.h"
#import "YWAllAlbumsViewController.h"
#import "YWMorePeopleViewController.h"
#import "AlbumDetailViewController.h"
#import "ZCSPlayMusicViewController.h"
#import "ZCSListenListListModel.h"


@interface YWAnchorIntroduceViewController ()<UITableViewDelegate, UITableViewDataSource, Return, EnterFollowVC>

@property (nonatomic, strong) UITableView *anchorMessageTableView;

@property (nonatomic, strong) YWHeaderView *headView;
//发布的专辑
@property (nonatomic, assign) NSInteger albumsCount;
//发布的声音
@property (nonatomic, assign) NSInteger tracks;

@property (nonatomic, retain) NSMutableArray *albumArray;

@property (nonatomic, retain) NSMutableArray *voiceArray;

@property (nonatomic, retain) NSMutableArray *playArray;

@property (nonatomic, assign) NSInteger maxPage;

@end

@implementation YWAnchorIntroduceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;

    [self getView];
    [self getData];
}

- (void)viewWillDisappear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = NO;
}

- (void)viewWillAppear:(BOOL)animated
{
    self.navigationController.navigationBar.hidden = YES;
}

- (void)enterFollowVC:(NSString *)string
{
    YWMorePeopleViewController *pvc = [[YWMorePeopleViewController alloc] init];
    pvc.typeString = string;
    pvc.uid = _uid;
    [self.navigationController pushViewController:pvc animated:YES];
}

- (void)getView
{
    self.anchorMessageTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, -20, SCREENWIDTH, SCREENHEIGHT + 20) style:UITableViewStyleGrouped];
    _anchorMessageTableView.delegate = self;
    _anchorMessageTableView.dataSource = self;
    _anchorMessageTableView.bounces = NO;
    _anchorMessageTableView.sectionFooterHeight = 0;
    _anchorMessageTableView.separatorStyle = NO;
    [self.view addSubview:_anchorMessageTableView];
    
    [_anchorMessageTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"cell"];
    
    self.headView = [[YWHeaderView alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 300 * FITHEIGHT)];
    _headView.delegate = self;
    _headView.enterFollowingDelegate = self;
    _anchorMessageTableView.tableHeaderView = _headView;
    
    [_anchorMessageTableView registerNib:[UINib nibWithNibName:@"ZCSTipsReTableViewCell" bundle:nil] forCellReuseIdentifier:@"TipCell"];
    [_anchorMessageTableView registerClass:[YWAnchorIntroduceAlbumTableViewCell class] forCellReuseIdentifier:@"moreCell"];
    [_anchorMessageTableView registerClass:[YWAnchorVoiceTableViewCell class] forCellReuseIdentifier:@"voiceCell"];
}

- (void)returnLastVC
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row < 2) {
            return 90;
        }
        return 30;
    }
    else
    {
        YWAnchorVoiceModel *voiceModel = [_voiceArray objectAtIndex:indexPath.row];
        if ([voiceModel.title length] > 17) {
            return 100;
        }
        return 80;
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        if (_albumArray.count > 2) {
            return 3;
        }
        else
        {
            return _albumArray.count;
        }
    }
    return _voiceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        if (indexPath.row < 2) {
            ZCSTipsReTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"TipCell"];
            cell.introduceAlbumsModel = [_albumArray objectAtIndex:indexPath.row];
            return cell;
        }
        else
        {
            YWAnchorIntroduceAlbumTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"moreCell"];
            cell.moreLabel.text = @"查看全部专辑";
            return cell;
        }
    }
    YWAnchorVoiceTableViewCell *voiceCell = [tableView dequeueReusableCellWithIdentifier:@"voiceCell"];
    voiceCell.voiceModel = [_voiceArray objectAtIndex:indexPath.row];
    return voiceCell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    if (section == 0) {
        UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 30 * FITHEIGHT)];
        textlabel.text = [NSString stringWithFormat:@"   发布的专辑(%ld)", _albumsCount];
        return textlabel;
    }
    else
    {
        UILabel *textlabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, SCREENWIDTH, 30 * FITHEIGHT)];
        textlabel.text = [NSString stringWithFormat:@"   发布的声音(%ld)", _tracks];
        return textlabel;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 30;
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 0;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0) {
        if (indexPath.row == 2) {
            YWAllAlbumsViewController *avc = [[YWAllAlbumsViewController alloc] init];
            avc.uid = _uid;
            [self.navigationController pushViewController:avc animated:YES];
        }
        else
        {
            AlbumDetailViewController *album = [[AlbumDetailViewController alloc] init];

            album.albmId = [[self.albumArray objectAtIndex:indexPath.row] albumId];
            album.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:album animated:YES];
        }
    }
    else
    {
        ZCSPlayMusicViewController *play = [ZCSPlayMusicViewController sharePlayVC];
        play.maxPage = self.maxPage;
        play.dataArray = self.playArray;
        play.musicIndex = indexPath.row;
        play.uid = self.uid;
        [self presentViewController:play animated:YES completion:nil];
    }
}

- (void)getData
{
    [self getAnchorMessage];
    self.albumArray = [NSMutableArray array];
    [self getTracks];
    self.voiceArray = [NSMutableArray array];
    self.playArray = [NSMutableArray array];
    [self getAnchorVoice];
}

- (void)getAnchorMessage
{
    NSString *urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/others/ca/homePage?device=android&statEvent=pageview%%2Fuser%%4015874143&statModule=热门&statPage=tab%%40发现_主播&statPosition=1&toUid=%ld", _uid];
    
    [LTNetTool GetNetWithURL:urlString Body:nil Header:nil Response:LTJSON Success:^(id result) {
        _headView.messageModel = [[YWAnchorMessageModel alloc] initWithDic:result];
        _albumsCount = [[result objectForKey:@"albums"] integerValue];
        _tracks = [[result objectForKey:@"tracks"] integerValue];
        [_anchorMessageTableView reloadData];
    } Failure:^(NSError *error) {
        NSLog(@"请求失败");
    }];
}

- (void)getTracks
{
    NSString *urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/v1/artist/albums?toUid=%ld", _uid];
        [LTNetTool GetNetWithURL:urlString Body:nil Header:nil Response:LTJSON Success:^(id result) {
        NSArray *tempArray = [result objectForKey:@"list"];
        for (NSDictionary *dic in tempArray) {
            YWAnchorIntroduceAlbumsModel *model = [[YWAnchorIntroduceAlbumsModel alloc] initWithDic:dic];
            [_albumArray addObject:model];
        }
        [_anchorMessageTableView reloadData];
        
    } Failure:^(NSError *error) {
        NSLog(@"请求失败");
    }];
}

- (void)getAnchorVoice
{
    NSString *urlString = [NSString stringWithFormat:@"http://mobile.ximalaya.com/mobile/v1/artist/tracks?device=android&pageId=1&toUid=%ld&track_base_url=http://mobile.ximalaya.com/mobile/v1/artist/tracks", _uid];
    [LTNetTool GetNetWithURL:urlString Body:nil Header:nil Response:LTJSON Success:^(id result) {
        _maxPage = [result[@"maxPageId"] integerValue];
        NSArray *tempArray = [result objectForKey:@"list"];
        for (NSDictionary *dic in tempArray) {
            YWAnchorVoiceModel *voiceModel = [[YWAnchorVoiceModel alloc] initWithDic:dic];
            ZCSListenListListModel *model = [[ZCSListenListListModel alloc] initWithDic:dic];
            [_playArray addObject:model];
            [_voiceArray addObject:voiceModel];
        }
        [_anchorMessageTableView reloadData];
    } Failure:^(NSError *error) {
        NSLog(@"请求失败");
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
